<template>
	<view class="addEditForm">
		<gui-back v-if="optionDisabled"></gui-back>
		<view class="data-form bgfff">
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30 require">章节名称</text>
				<view class="data-form-input">
					<input type="text" :disabled="optionDisabled" v-model="dataForm.name"
						:placeholder="optionDisabled?'-':'请输入章节名称'" placeholder-class="plStyle" />
				</view>
			</view>
		</view>
		<view class="right-area-footer2 flex-ac bgfff" v-if="!optionDisabled">
			<view class="saveBtn cancleBtn" @tap="handleCancle">取消</view>
			<view class="saveBtn" @tap="handleSubmit">保存</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				did: '',
				dataForm: { //表单
				    curriculumId:'', //课程id
				    id:'',
					name: ''
				},
				optionDisabled: false,
			}
		},
		onLoad(options) {
			console.log('options', options);
			this.did = options.id || ''
			this.curriculumId = options.curriculumId?options.curriculumId:''
			this.optionDisabled = options.op == 'look' ? true : false
			let title = options.op == 'look' ? '详情' : options.op == 'add' ? '新增' : '编辑'
			uni.setNavigationBarTitle({
				title
			})
			if(this.did) {
				this.loadDetail()
			}
		},
		methods: {
			loadDetail() {
				this.$http.get(`/training/chapter/${this.did}`).then(res => {
					if (res.code == 200) {
						console.log('res',res);
						let result = res.data
						Object.keys(this.dataForm).forEach(key=>this.dataForm[key]=result[key])
						console.log('this.dataForm',this.dataForm);
					}
				})
			},
			handleCancle() {
				this.$u.back()
			},
			handleSubmit() {
				if (!this.dataForm.name) return this.$u.msg("请输入章节名称！");
				this.realSumbit();
			},
			realSumbit() {
				if (this.did) {
					this.dataForm.id = this.did
					this.$http.put('/training/chapter', this.dataForm).then(res => {
						if (res.code == 200) {
							uni.$emit('reFlashList')
							this.$u.msg('修改成功!')
							this.$u.back(1, 1000)
						}
					})
				} else {
					this.$http.post('/training/chapter', this.dataForm).then(res => {
						if (res.code == 200) {
							uni.$emit('reFlashList')
							this.$u.msg('新增成功!')
							this.$u.back(1, 1000)
						}
					})
				}
			},
		}
	}
</script>

<style>

</style>